/*
 * @Author: your name
 * @Date: 2021-03-04 14:57:07
 * @LastEditTime: 2021-04-15 13:06:48
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /rsweb/src/request/index.js
 */
import Vue from 'vue'
import VueAxios from 'vue-axios'
import axios from 'axios'
import Qs from 'qs'
import {get} from 'lodash'
import router from '@/router'

Vue.use(VueAxios, axios)
// 请求拦截器
axios.interceptors.request.use(function (config) {
    config.headers = {
        'Content-Type': 'application/json;charset=utf-8'
    }
    let flag
    if (config.method == 'get') {
        flag = (config.params && config.params.tf) || ''
    } else {
        flag = (config.data && config.data.tf) || ''
    }
    switch (flag) {
        case 1:
            //Json
            config.headers = {
                'Content-Type': 'application/json;charset=utf-8'
            }
            break
        case 2:
            //form
            config.data = Qs.stringify(config.data)

            config.headers = {
                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
            break
        case 3:
            //上传文件流
            //   config.data = Qs.stringify(config.data);
            config.headers = {
                'Content-Type': 'multipart/form-data;charset=utf-8'
            }
            break

        default:
            config.headers = {
                'Content-Type': 'application/json;charset=utf-8'
            }
            break
    }
    return config
}, function (error) {
    console.log(error)
    throw '你发送的请求体有问题'
})
// 响应拦截器
axios.interceptors.response.use(function (response) {
    var el = new Vue()
    let {url} = response.config;
    console.log(url);
    let {code, data, message, success} = response.data

    if (code === 1 && success) {
        return response.data || {}
    } else if (code === 10002 || code === 0 || code === -3 || code === 10301 || code === 1 || code === 10302 || code === 19999) {
        el.$notify.error({
            title: '提示', message: message
        })
        return response.data || {}
    } else if (code === 10107 || (!success && url === '/temp/sso/pengzhouLogin')) {
        el.$notify.error({
            title: '提示', message: message
        })
        router.replace({name: "Login"})
    } else {
        el.$notify.error({
            title: '提示', message: message
        })
        return Promise.reject(response.data || {})
    }
}, function (error) {
    // Do something with response error
    var el = new Vue()
    // el.$confirm("请联系客服邮箱：XXX,寻求帮助", "提示", {
    //   confirmButtonText: "确定",
    //   showCancelButton: false,
    //   type: "warning"
    // })
    //   .then(() => {})
    //   .catch(() => {});
    // return _.get(response, 'data', {});
})

export default axios
